package com.sskj.gocoin.v.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.TimeUtil;
import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.p.activity.BindEmailPresenter;
import com.sskj.gocoin.utils.CheckUtil;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.utils.SPUtils;
import com.sskj.gocoin.utils.ToastUtil;
import com.sskj.gocoin.v.base.BaseActivity;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class BindEmailActivity extends BaseActivity<BindEmailPresenter> {

    @BindView(R2.id.bind_email_tv)
    EditText mBindEmailTv;
    @BindView(R2.id.bind_code_tv)
    EditText mCodeTv;
    @BindView(R2.id.toolBar)
    ToolBarLayout mToolBar;
    @BindView(R2.id.title)
    TextView mTitle;
    @BindView(R2.id.bind_email_submit)
    Button mBindEmailSubmit;
    @BindView(R2.id.bt_send_email_code)
    Button mSendEmailCode;
    private String mobile;
    private Disposable timeDis;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_bind_email;
    }

    @Override
    public BindEmailPresenter getPresenter() {
        return new BindEmailPresenter();
    }

    @Override
    protected void onViewBinding() {
        mobile = (String) SPUtils.get(MyAppLication.getApplication(), Constants.SP_MOBILE, "");

        mToolBar.setTitle(App.INSTANCE.getString(R.string.fabimineJavaBindEmailActivity2));
        mToolBar.setLeftButtonText(App.INSTANCE.getString(R.string.fabimineJavaBindEmailActivity3));
        mToolBar.setLeftButtonOnClickLinster(v -> finish());
        ClickUtil.click(mBindEmailSubmit, () -> {
            String email = mBindEmailTv.getText().toString().trim();
            String code = mCodeTv.getText().toString().trim();
            if (TextUtils.isEmpty(email)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabimineJavaBindEmailActivity4));
                return;
            }
            if (!CheckUtil.isEmail(email)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strBindEmailActivity202));
                return;
            }
            if (TextUtils.isEmpty(code)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabimineJavaBindEmailActivity5));
                return;
            }
            mPresenter.requestBindEmail(code, email, mobile);
        });
        ClickUtil.click(mSendEmailCode, () -> {
            String email = mBindEmailTv.getText().toString().trim();
            if (TextUtils.isEmpty(email)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabimineJavaBindEmailActivity4));
                return;
            }
            if (!CheckUtil.isEmail(email)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strBindEmailActivity202));
                return;
            }
            mPresenter.sendEmai(email);
        });

        mBindEmailTv.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (CheckUtil.isEmail(s.toString())) {
                    mBindEmailSubmit.setEnabled(true);
                } else {
                    mBindEmailSubmit.setEnabled(false);
                }
            }
        });
    }


    public void onResponseSuccess() {
        setResult(RESULT_OK, new Intent().putExtra("resultData", mBindEmailTv.getText().toString()));
        finish();
    }

    public void sendCheckCodeSuccess() {
        DisposUtil.close(timeDis);
        timeDis = TimeUtil.newTime()
                .subscribe(aLong -> {
                    if (aLong > 0) {
                        mSendEmailCode.setText(aLong + " s");
                    } else {
                        mSendEmailCode.setText(App.INSTANCE.getString(R.string.fabimineJavaBindEmailActivity9));
                        mSendEmailCode.setEnabled(true);
                    }
                });
    }

    public void sendCheckCodeFail() {
        mSendEmailCode.setEnabled(true);
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(timeDis);
        super.onDestroy();
    }

}
